<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author PR
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ ddate: record.ddate })">
          {{ record.ddate }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsReportVmoldKeepMonthList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { vmoldKeepMonthDelete, vmoldKeepMonthListData } from '/@/api/mold/report/vmoldKeepMonth';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormImport from './formImport.vue';

  const { t } = useI18n('report.vmoldKeepMonth');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('工装保养汇总管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('登记日期'),
        field: 'ddate',
        component: 'RangePicker',
        componentProps: {
          picker: 'month', // 月份选择器
          format: 'YYYY-MM',
          valueFormat: 'YYYY-MM',
          showTime: false,
        },
      },
      {
        label: t('模具保养项目'),
        field: 'keepDesc',
        component: 'Input',
      },
      {
        label: t('工装代码'),
        field: 'caddcode',
        component: 'Input',
      },
      {
        label: t('工装编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('工装编码尾号'),
        field: 'lastnum',
        component: 'Input',
      },
      {
        label: t('工装名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('保养人员'),
        field: 'userName',
        component: 'Input',
      },
      {
        label: t('保养时间'),
        field: 'keepTime',
        component: 'Input',
      },
      {
        label: t('加工产品'),
        field: 'cinvaddcode',
        component: 'Input',
      },
      {
        label: t('加工数量'),
        field: 'iqty',
        component: 'Input',
      },
      {
        label: t('设备编码'),
        field: 'devName',
        component: 'Input',
      },
      {
        label: t('工序'),
        field: 'operCode',
        component: 'Input',
      },
    ],
    fieldMapToTime: [['ddate', ['ddate_gte', 'ddate_lte'],'YYYY-MM']],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('登记日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('模具保养项目'),
      dataIndex: 'keepDesc',
      key: 'a.keep_desc',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工装代码'),
      dataIndex: 'caddcode',
      key: 'a.caddcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工装编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工装编码尾号'),
      dataIndex: 'lastnum',
      key: 'a.lastnum',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工装名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('保养人员'),
      dataIndex: 'userName',
      key: 'a.user_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('保养时间'),
      dataIndex: 'keepTime',
      key: 'a.keep_time',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('加工产品'),
      dataIndex: 'cinvaddcode',
      key: 'a.cinvaddcode',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('加工数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('设备编码'),
      dataIndex: 'devName',
      key: 'a.dev_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('工序'),
      dataIndex: 'operCode',
      key: 'a.oper_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm }] = useTable({
    api: vmoldKeepMonthListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/report/vmoldKeepMonth/exportData',
      target: '_self',
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const res = await vmoldKeepMonthDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
